﻿using DimensionsHelper.Common.Utils;

namespace DimensionsHelper.ExcelAddIn.Core.Sql;

/// <summary>
/// SQL脚本生成器的表头标识符设置类
/// </summary>
public class HeaderIdentifiers
{

    private HashSet<string>? _id;
    private HashSet<string>? _field;
    private HashSet<string>? _value;
    private HashSet<string>? _valueToRemove;
    private HashSet<string>? _extra;

    /// <summary>
    /// ID列的标识符
    /// </summary>
    public HashSet<string>? Id
    {
        get => _id;
        set => _id = CheckStringComparer(value);
    }

    /// <summary>
    /// 字段列的标识符
    /// </summary>
    public HashSet<string>? Field
    {
        get => _field;
        set => _field = CheckStringComparer(value);
    }

    /// <summary>
    /// 值列的标识符
    /// </summary>
    public HashSet<string>? Value
    {
        get => _value;
        set => _value = CheckStringComparer(value);
    }

    /// <summary>
    /// 移除值列的标识符
    /// </summary>
    public HashSet<string>? ValueToRemove
    {
        get => _valueToRemove;
        set => _valueToRemove = CheckStringComparer(value);
    }

    /// <summary>
    /// 额外列的标识符
    /// </summary>
    public HashSet<string>? Extra
    {
        get => _extra;
        set => _extra = CheckStringComparer(value);
    }


    private static void SetProperty(ref HashSet<string>? field, string? value)
    {
        if (string.IsNullOrEmpty(value))
        {
            return;
        }

        if (field == null)
        {
            field = value.GetIdentifiers();
        }
        else
        {
            field.AddRange(value.GetIdentifiers());
        }
    }


    private HashSet<string>? CheckStringComparer(HashSet<string>? set)
    {
        if (set == null)
        {
            return null;
        }

        if (set.Comparer != StringComparer.OrdinalIgnoreCase)
        {
            set = new HashSet<string>(set, StringComparer.OrdinalIgnoreCase);
        }

        return set;
    }


    public HeaderIdentifiersTransmission Transmit()
    {
        return new HeaderIdentifiersTransmission
        {
            Id = Id != null ? string.Join(Environment.NewLine, Id) : string.Empty,
            Field = Field != null ? string.Join(Environment.NewLine, Field) : string.Empty,
            Value = Value != null ? string.Join(Environment.NewLine, Value) : string.Empty,
            ValueToRemove = ValueToRemove != null ? string.Join(Environment.NewLine, ValueToRemove) : string.Empty,
            Extra = Extra != null ? string.Join(Environment.NewLine, Extra) : string.Empty
        };
    }


    public void Join(HeaderIdentifiersTransmission transmission)
    {
        SetProperty(ref _id, transmission.Id);
        SetProperty(ref _field, transmission.Field);
        SetProperty(ref _value, transmission.Value);
        SetProperty(ref _valueToRemove, transmission.ValueToRemove);
        SetProperty(ref _extra, transmission.Extra);
    }

}


public static class Extensions
{
    internal static HashSet<string> GetIdentifiers(this string? value)
    {
        if (string.IsNullOrEmpty(value))
        {
            return new HashSet<string>(StringComparer.OrdinalIgnoreCase);
        }

        var identifiers = value!
            .Replace("\r\n", "\n")
            .Split('\n')
            .Select(e => e.Trim())
            .Where(e => !string.IsNullOrEmpty(e));

        return new HashSet<string>(identifiers, StringComparer.OrdinalIgnoreCase);
    }
}
